package com.yanyu.space.sys.service.test.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yanyu.space.sys.bean.po.Dept;
import com.yanyu.space.sys.dao.DeptMapper;
import com.yanyu.space.sys.service.impl.DeptServiceImpl;
import com.yanyu.space.sys.service.test.ITxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gz
 */
@Service
@Slf4j
public class TxServiceImpl implements ITxService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTx() throws InterruptedException {
        long cnt = deptMapper.selectCount(Wrappers.<Dept>lambdaQuery());
        log.info("testTx一共查出{}数据", cnt);
        Thread.sleep(10000);
        ((TxServiceImpl) AopContext.currentProxy()).testTx2();
        ((TxServiceImpl) AopContext.currentProxy()).testTx3();
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void testTx2() {
        long cnt = deptMapper.selectCount(Wrappers.<Dept>lambdaQuery());
        log.info("testTx2一共查出{}数据", cnt);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void testTx3() {
        long cnt = deptMapper.selectCount(Wrappers.<Dept>lambdaQuery());
        log.info("testTx3{}数据", cnt);
    }
}
